package com.abel.bigwater.dataserver.model.ewide

import com.abel.bigwater.dataserver.model.PacketNotEnoughException
import io.netty.buffer.ByteBuf

class KmingResponseData(_type: KmingDataType) : TermData(_type, 2) {

    var sessionReponseType: SessionReponseType? = null

    override fun bufLength() = 2

    override fun parseBuf(buf: ByteBuf): Boolean {
        if (buf.readableBytes() < 2) {
            throw PacketNotEnoughException("should be 2 bytes long")
        }

        assert(type == KmingDataType.findByCode(buf.readUnsignedByte()))

        sessionReponseType = SessionReponseType.findByCode(buf.readByte())

        return true
    }

    override fun pack(buf: ByteBuf) {
        buf.alloc().buffer().apply {
            writeByte(type.code.toInt())
            writeByte(sessionReponseType!!.code.toInt())

            buf.writeBytes(this)
        }
    }

    override fun toString(): String {
        return "{type:$type,response:$sessionReponseType}"
    }
}